package com.btr.ivle;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.btr.ivle.R;
import com.btr.ivle.backgroud.AsyncTaskCompleteListener;
import com.btr.ivle.backgroud.GetForumThreadTask;
import com.btr.ivle.data.ForumData;
import com.btr.ivle.data.IVLEToken;
import com.btr.ivle.helper.ForumPostListAdapter;
import com.btr.ivle.helper.IsNetworkAvailable;

public class ViewForumPostActivity extends SherlockListActivity implements 
	AsyncTaskCompleteListener<String>{
	
	private MenuItem item;
	private Context context = this;
	private Activity myApp = this;
	private GetForumThreadTask getForumThreadTask;
	private String ID;
	private boolean hasNewPost;
	private List<Map<String, String>> list;
	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Sherlock_Light);

        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(false);
        
        Intent intent = getIntent();
	    ID = intent.getStringExtra(ListForumPostActivity.MSG_PostID);
	    hasNewPost = intent.getBooleanExtra("hasNewPost", false);
	    //ForumID = intent.getStringExtra(ListForumPostActivity.MSG_ForumID);
	    setListView();
    }
	
	public void getForum(){
        if(!IVLEToken.isOffline()){
	        if(IsNetworkAvailable.check(context)){
	        	item.setActionView(R.layout.progressbarindeterminatesmall);
	        	getForumThreadTask = new GetForumThreadTask(context);
		        getForumThreadTask.execute(ID);
	        }else{
	        	IVLEToken.setOffline(true);
	        	Toast.makeText(context, "Sorry, currently OFFLINE MODE dose NOT support Furoms." , Toast.LENGTH_LONG).show();
	        }
        }else{
        	Toast.makeText(context, "Sorry, currently OFFLINE MODE dose NOT support Furoms." , Toast.LENGTH_LONG).show();
        }
    }
	
	public void onDestroy(){
    	if(this.getForumThreadTask!=null)
    		this.getForumThreadTask.cancel(true);
    	super.onDestroy();
    }
	
	public void onAsyncTaskComplete(String result) {
		setListView();
		if(item!=null)
			item.setActionView(null);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.layout.menu_refresh, menu);
        item = menu.findItem(R.id.refresh);
        if(hasNewPost)
        	getForum();
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(final MenuItem item) 
    {
    	switch (item.getItemId()) {
        	case android.R.id.home:
        		super.onBackPressed();
        		break;
        	case R.id.refresh:
	        	this.item = item;
	        	getForum();
	        	break;
    	}
          return true;
    }
	
	private void setListView(){
		list = ForumData.get(ID);
	    
	    ForumPostListAdapter adapter = new ForumPostListAdapter(this, list, myApp);
        setListAdapter(adapter);
        ListView lv = getListView();
        lv.setDivider(new ColorDrawable(R.color.gray));
        lv.setTextFilterEnabled(true);
        lv.setBackgroundColor(getResources().getColor(R.color.white));
	}
	
	@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      switch(requestCode) {
        case 0 :
        	if (resultCode == Activity.RESULT_OK) {
        		boolean posted = data.getBooleanExtra(PostForumActivity.MSG_PSOTED, false);
        		if(posted){
        			item.setActionView(R.layout.progressbarindeterminatesmall);
        			getForum();
        		}
        	}
        	break;
      }
    }
	

}
